#!/usr/bin/env python
# Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import string
from socket import *

PAYLOAD = """title=%s
message=%s
password=%s
.
"""

def remote_input(inputMessage, isPassword = False):
    if not ("GAEDT_USER_INPUT_SERVICE_PORT" in os.environ):
        raise Exception("(gaedt) User input service port environment variable not found.")

    serverHost = "localhost"
    serverPort = string.atoi(os.environ["GAEDT_USER_INPUT_SERVICE_PORT"])
    title = "User Input (App Update)"
    message = inputMessage
    password = u"false"
    if isPassword:
        password = u"true"
    payload = PAYLOAD % (title, message, password)

    sock = socket(AF_INET, SOCK_STREAM)
    try:
        sock.connect( (serverHost, serverPort) )
        sock.send(payload)
        data = sock.recv(1024)
        if data == u"_zgae_cancel_operation":
            raise Exception("User cancelled action.")
        return data
    finally:
        sock.close()
# end remote_input()


def remote_pass_input(passMessage):
    return remote_input(passMessage, True)
# end remote_pass_input()
